<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/9/28
 * Time: 9:19
 */

namespace App\Game;

use Carbon\Carbon;
use App\Game\Traits\SaTrait;

class sa extends Game
{
    use SaTrait;

    private $platName = 'sa';



    public function __construct()
    {

        $this->header = [
            "Accept" => "application/json",
            "Content-Type" => "application/json",
        ];
        $this->adds = [
            'main_game_plat_code' => $this->platName,
            'status' => 1
        ];
    }

    public function getLength()
    {
        return $this->length;
    }

    public function getPre()
    {
        return $this->pre;
    }

    /**
     * gameapi
     * 2018/9/19 16:15
     * Administrator
     * __get
     *
     * @param string $key
     * @return mixed
     */
    public function __get($key)
    {
        return $this->$key;
    }

    public function __set($key, $value)
    {
        $this->$key = $value;
    }


    /**
     * gameapi
     * 2018/9/24 14:16
     * Administrator
     * curlCreateMember
     * 创建用户
     * @param $accountUserName
     * @param $password
     * @param $request
     * @return array
     */
    public function curlCreateMember($accountUserName, $password, $request)
    {
        $action = [
            'command' => 'addPlayer',
            'parameters' => [
                'loginname' => $accountUserName,
            ]
        ];

        $output = $this->saRequest($action);
        $output = json_decode($output, true);

        if (array_key_exists('status', $output)) {
            if ($output['status'] == 200) {
                return ['success' => true, 'data' => ['accountUserName' => $accountUserName, 'password' => $password], 'message' => ''];
            } else {
                return ['success' => false, 'data' => [], 'message' => '创建用户失败，错误原因：' . $output['result']];
            }
        } else {
            return ['success' => false, 'data' => [], 'message' => '请求平台失败，状态未知'];
        }
    }

    /**
     * gameapi
     * 2018/9/24 14:15
     * Administrator
     * curlGetBalance
     * 查询余额
     * @param $accountUserName
     * @return array
     */
    public function curlGetBalance($accountUserName)
    {
        $action = [
            'command' => 'queryCredit',
            'parameters' => [
                'loginname' => $accountUserName,
            ]
        ];

        $output = $this->saRequest($action);
        $output = json_decode($output, true);

        if ($output['status'] == 200 && $output['errorMsg'] == '操作成功') {
            return ['success' => true, 'data' => ['balance' => $output['data']['credit']], 'message' => ''];
        } else {
            return ['success' => false, 'data' => [], 'message' => '查询余额失败，错误原因：' . $output['errorMsg']];
        }
    }


    //转入游戏
    public function curlTransferIn($accountUserName, $price, $transferid)
    {
        $transferid = 'IN' . Carbon::now()->format('YmdHis') . $accountUserName;
        $action = [
            'command' => 'transferIn',
            'parameters' => [
                'loginname' => $accountUserName,
                'orderId' => $transferid,
                'credit' => $price,
            ]
        ];

        $output = $this->saRequest($action);
        $output = json_decode($output, true);

        if (array_key_exists('status', $output)) {
            if ($output['status'] == 200 && $output['errorMsg'] == '操作成功') {
                return ['success' => true, 'data' => ['transferid' => $transferid, 'transfer_state' => 1], 'message' => ''];
            } else {
                return ['success' => false, 'data' => ['transfer_state' => 0], 'message' => '转入失败，错误原因：' . $output['errorMsg']];
            }
        } else {
            return ['success' => false, 'data' => ['transfer_state' => -1], 'message' => '请求平台失败，状态未知'];
        }
    }

    //转出游戏
    public function curlTransferTo($accountUserName, $price, $transferid)
    {
        $transferid = 'OUT' . Carbon::now()->format('YmdHis') . $accountUserName;
        $action = [
            'command' => 'transferOut',
            'parameters' => [
                'loginname' => $accountUserName,
                'orderId' => $transferid,
                'credit' => $price,
            ]
        ];

        $output = $this->saRequest($action);
        $output = json_decode($output, true);

        if (array_key_exists('status', $output)) {
            if ($output['status'] == 200 && $output['errorMsg'] == '操作成功') {
                return ['success' => true, 'data' => ['transferid' => $transferid, 'transfer_state' => 1], 'message' => ''];
            } else {
                return ['success' => false, 'data' => ['transfer_state' => 0], 'message' => '转出失败，错误原因：' . $output['errorMsg']];
            }
        } else {
            return ['success' => false, 'data' => ['transfer_state' => -1], 'message' => '请求平台失败，状态未知'];
        }
    }

    //查询订单状态
    public function curlCheckTransfer($request)
    {
        return ['success' => false, 'data' => ['transfer_state' => -1], 'message' => '此平台没有查询订单功能'];
    }

    //踢线
    public function curlKick($accountUserName)
    {
        return ['success' => false, 'data' => [], 'message' => '此平台没有踢线功能'];
    }

    /**
     * gameapi
     * 2018/9/24 16:35
     * Administrator
     * curlJoinGame
     * 进入游戏
     * @param $request
     * @return array
     */
    public function curlJoinGame($request)
    {
        $action = [
            'command' => 'getLoginURL',
            'parameters' => [
                'loginname' => $request->accountUserName,
                'isMobile' => 0,
            ]
        ];

        $output = $this->saRequest($action);
        $output = json_decode($output, true);

        if (array_key_exists('status', $output)) {
            if ($output['status'] == 200 && $output['errorMsg'] == '操作成功') {
                return ['success' => true, 'data' => ['url' => $output['data']['loginUrl']], 'message' => ''];

            } else {
                return ['success' => false, 'data' => [], 'message' => '进入游戏失败，错误原因：' . $output['errorMsg']];
            }
        } else {
            return ['success' => false, 'data' => [], 'message' => '请求平台失败，状态未知'];
        }
    }


    /**
     * gameapi
     * 2018/9/24 16:35
     * Administrator
     * curlJoinMobileGame
     * 手机端进入游戏
     * @param $request
     * @return array
     */
    public function curlJoinMobileGame($request)
    {
        $action = [
            'command' => 'getLoginURL',
            'parameters' => [
                'loginname' => $request->accountUserName,
                'isMobile' => 1,
            ]
        ];

        $output = $this->saRequest($action);
        $output = json_decode($output, true);

        if (array_key_exists('status', $output)) {
            if ($output['status'] == 200 && $output['errorMsg'] == '操作成功') {
                return ['success' => true, 'data' => ['url' => $output['data']['loginUrl']], 'message' => ''];

            } else {
                return ['success' => false, 'data' => [], 'message' => '进入游戏失败，错误原因：' . $output['errorMsg']];
            }
        } else {
            return ['success' => false, 'data' => [], 'message' => '请求平台失败，状态未知'];
        }
    }


    //游戏抓单
    public function curlGetBetRecord(Carbon $startTime, Carbon $endTime)
    {
        $data = [];
        $start_time = $startTime->toDateTimeString();
        $end_time = $endTime->toDateTimeString();

        $action = [
            'command' => 'getBetRecords',
            'parameters' => [
                'startDate' => $start_time,
                'endDate' => $end_time,
            ]
        ];

        $output = $this->saRequest($action);
        $output = json_decode($output, true);
        if (array_key_exists('status', $output)) {
            if ($output['status'] == 200 && $output['errorMsg'] == '操作成功') {
                $result = $output['data'];
                if (count($result['bets']) >= 1) {
                    $data = array_merge($data, $result['bets']);
                }

            }
        }

        return $data;
    }
}